﻿import { Component, EventEmitter, Output } from '@angular/core';
import { Router } from '@angular/router';
import { FormGroup, FormControl, Validators, FormBuilder }
    from '@angular/forms';
import { Http, Headers } from '@angular/http';
import { ApiService } from '../../services/app.service.api';

@Component({
    selector: 'registercustomer',
    templateUrl: './app/components/customers/registercustomer.component.html'
})
export class RegisterCustomerComponent {
    addCustomerForm: FormGroup;
    @Output() showUserInfo = new EventEmitter();

    submission = {
        successMessages: ['Successfull submission will appear here.'],
        errorMessages: ['Submition errors will appear here.']
    };
    constructor(public fb: FormBuilder, private apiService: ApiService, private router: Router) {
        this.addCustomerForm = fb.group({
            'firstName': [null, Validators.required],
            'lastName': [null, Validators.required],
            'email': [null, Validators.required],
            'identityCard': [null, Validators.required],
            'dateOfBirth': [null, Validators.required],
            'mobile': [null, Validators.required],
        });

    }
    Register(form: any): void {
        this.apiService.post('/api/customers/register', form, res => {
            this.submission.errorMessages = ['Submition errors will appear here.'];
            let customerRegistered: any = res.json();
            this.submission.successMessages = [];
            this.submission.successMessages.push(form.lastName + ' has been successfully registed');
            this.submission.successMessages.push('Check ' + customerRegistered.UniqueKey + ' for reference number');
            //清空表单数据
            this.addCustomerForm.reset();
        }, error => {
            if (error.status == '400') {
                console.log(error._body);
                this.submission.errorMessages = error.json();
            }     
            else
                this.submission.errorMessages = error.statusText;
        });
    }

    test() {
        this.submission.successMessages.push('test');
        this.addCustomerForm.reset();
    }
    openDatePicker($event) {
        $event.preventDefault();
        $event.stopPropagation();

    };
}




